from datetime import date
from django.db import models
from django.utils import timezone

from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill

from auditlog.models import AuditlogHistoryField
from auditlog.registry import auditlog


class Store(models.Model):
    name = models.CharField("仓库", max_length=128, unique=True)

    class Meta:
        verbose_name = "仓库"
        verbose_name_plural = verbose_name
        db_table = "stroe"

    def __str__(self):
        return self.name


class ServeGift(models.Model):
    name = models.CharField("商品名称", max_length=512)
    brand = models.CharField("品牌", max_length=256)
    specification = models.CharField("规格", max_length=256)
    count = models.PositiveSmallIntegerField("数量", default=1)
    unit = models.CharField("单位", max_length=8)
    poster = models.ImageField(upload_to='gifts/%Y-%m', verbose_name='封面图',
                               default='gifts/default.png')
    poster_thumbnail = ImageSpecField(source='poster', processors=[ResizeToFill(60, 30)],
                                      format='JPEG', options={'quality': 100})

    place = models.CharField("存放位置", max_length=256, null=True, blank=True)
    store = models.ForeignKey(Store, on_delete=models.SET_NULL, null=True, blank=True,
                              verbose_name="仓库", related_name="gifts")
    comment = models.TextField("备注", default='', blank=True)
    buy_date = models.DateField("购买时间", default=date.today)
    updated = models.DateTimeField("更新时间", auto_now=True)
    created = models.DateTimeField("创建时间", auto_now_add=True)
    history = AuditlogHistoryField()

    class Meta:
        verbose_name = "招待礼品"
        verbose_name_plural = verbose_name
        db_table = "gifts"

    def __str__(self):
        return f"{self.name} x{self.count}{self.unit}"

    def store_location(self):
        if self.store:
            return self.store.name if self.place is None else f"{self.store.name}-{self.place}"
        else:
            return '-'

    store_location.short_description = "存放位置"


class ConsumeRecord(models.Model):
    gift = models.ForeignKey(ServeGift, on_delete=models.CASCADE, verbose_name="商品", related_name="consume")
    recipient = models.CharField("领用人", max_length=32)
    department = models.CharField("部门", max_length=32)
    count = models.PositiveSmallIntegerField("数量", default=1)
    unit = models.CharField("单位", max_length=8)
    purpose = models.CharField("用途", max_length=512)
    created = models.DateTimeField("领用时间", default=timezone.now)
    history = AuditlogHistoryField()

    class Meta:
        verbose_name = "领用记录"
        verbose_name_plural = verbose_name
        db_table = "consume_record"

    def __str__(self):
        return f"{self.gift.name} {self.department}-{self.recipient} {self.count}{self.unit}"

    def amount(self):
        return f"{self.count}{self.unit}"

    amount.short_description = "数量/单位"

    def get_recipient(self):
        return f"{self.department}-{self.recipient}"

    get_recipient.short_description = '领用人'


class EnterStoreRecord(models.Model):
    gift = models.ForeignKey(ServeGift, on_delete=models.CASCADE, verbose_name="商品", related_name="enter")
    count = models.PositiveSmallIntegerField("数量", default=1)
    created = models.DateTimeField("入库时间", default=timezone.now)

    class Meta:
        verbose_name = "入库记录"
        verbose_name_plural = verbose_name
        db_table = "enter_record"


auditlog.register(ServeGift)
auditlog.register(ConsumeRecord)
